<?php

namespace App\Services;
use App\Models\Crm_Meet;
use App\Models\Crm_Member_Account;
use App\Traits\External;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Throwable;

class MeetService
{
    public function updateSlipStatus($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            $res = Crm_Meet::find($params['id']);
            $res->status = $params['status'];

            if(!$res->save()){
                throw new \Exception("更新Crm_Meet约见工单状态失败" );
            }
            DB::commit();

            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function meetOrderList($params)
    {
        $result = Crm_Meet::getFilter($params)->Nodel()->paginate($params['pageSize']);
        $list['total'] = $result->total();
        $result = External::optionResult($result,3);
        $list['list'] = $result;
        return $list;
    }

    public function createMeetOrder($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            $res = Crm_Meet::create([
                'agencyid' => $params['aid'],
                'mid' => $params['mid'],
                'otherid' => $params['otherid'],
                'type' => $params['type'],
                'mode' => $params['mode'],
                'meettime' => $params['meettime'],
                'option_id' => $params['uid'],
                'option_name' => $params['uname']
            ]);
            if(!$res){
                throw new \Exception("创建Crm_Meet约见工单失败" );
            }

            //若mode为1，则扣除约见人的约见次数
            if($params['mode'] == 1){
                $memberAccout = Crm_Member_Account::where('mid',$params['mid'])
                    ->where('agencyid',$params['aid'])
                    ->first();
                $memberAccout->meettimes = $memberAccout->meettimes - 1;
                if(!$memberAccout->save()){
                    throw new \Exception("扣除Crm_Member_Account约见人次数失败" );
                }
            }
            //若mode为2，则扣除约见人和被约见人的约见次数
            if($params['mode'] == 2) {
                $memberAccout = Crm_Member_Account::where('mid',$params['mid'])
                    ->where('agencyid',$params['aid'])
                    ->first();

                $memberAccout->meettimes = $memberAccout->meettimes - 1;
                if(!$memberAccout->save()){
                    throw new \Exception("扣除Crm_Member_Account约见人次数失败" );
                }

                $otherMemberAccout = Crm_Member_Account::where('mid',$params['otherid'])
                    ->where('agencyid',$params['aid'])
                    ->first();
                $otherMemberAccout->meettimes = $otherMemberAccout->meettimes - 1;
                if(!$memberAccout->save()){
                    throw new \Exception("扣除Crm_Member_Account被约见人次数失败" );
                }
            }
            DB::commit();
        }catch (Throwable $e){
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
        return '';
    }
}
